package service

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/dao"
	"lot_admin_api/internal/model"
	GenInt64Id "lot_admin_api/utility/genInt64IdOfString"
	"lot_admin_api/utility/paging"
	"lot_admin_api/utility/response"
)

type sAgreement struct{}

var (
	insAgreement = sAgreement{}
)

func Agreement() *sAgreement {
	return &insAgreement
}

// AgreementInfo  获取协议详情
func (s *sAgreement) AgreementInfo(ctx context.Context, req *model.AgreementIdReq) (res *model.AgreementInfoRes, err error) {
	r := g.RequestFromCtx(ctx) // 获取请求对象
	if err := dao.LotAgreement.Ctx(ctx).Where("id", req.Id).Scan(&res); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "查询失败")
	}
	return res, err
}

// AgreementList  获取协议详情
func (s *sAgreement) AgreementList(ctx context.Context, req *model.SearchAgreementListReq) (res *model.AgreementListRes, err error) {
	r := g.RequestFromCtx(ctx) // 获取请求对象
	startNum, endNum := paging.Paging(req.Paging)
	agreementList := make([]*model.AgreementInfoRes, 0)
	searchInfo := g.Map{
		"agreement_type": req.AgreementType,
	}
	if err = dao.LotAgreement.Ctx(ctx).OmitEmpty().WithAll().Where(searchInfo).Limit(startNum, endNum).OrderDesc("created_at").Scan(&agreementList); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	// 获取总条数
	countInfo, err := dao.LotAgreement.Ctx(ctx).OmitEmpty().WithAll().Where(searchInfo).Count()
	putCount := paging.PutCount(req.Paging, countInfo)
	res = &model.AgreementListRes{
		List:    agreementList,
		PutPage: putCount,
	}
	return res, err
}

// AddAgreement  添加协议
func (s *sAgreement) AddAgreement(ctx context.Context, req *model.AddAgreementRes) (res *model.AgreementIdRes, err error) {
	r := g.RequestFromCtx(ctx)
	agreementId := GenInt64Id.GenInt64IdOfString()
	saveInfo := &model.AgreementInfoRes{
		Id:            agreementId,
		AgreementType: req.AgreementType,
		AgreementBody: req.AgreementBody,
	}
	if findOne, _ := dao.LotAgreement.Ctx(ctx).WithAll().Where("agreement_type", req.AgreementType).One(); findOne != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "已经有该类型的协议")
	}
	if _, err = dao.LotAgreement.Ctx(ctx).Save(&saveInfo); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	res = &model.AgreementIdRes{
		Id: agreementId,
	}
	return res, err
}

// UpdateAgreement  更新协议
func (s *sAgreement) UpdateAgreement(ctx context.Context, req *model.AgreementInfoRes) (res *model.AgreementIdRes, err error) {
	r := g.RequestFromCtx(ctx)
	if _, err = dao.LotAgreement.Ctx(ctx).Where("id", req.Id).Update(&req); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	res = &model.AgreementIdRes{
		Id: req.Id,
	}
	return res, err
}

// DeleteAgreement  删除协议
func (s *sAgreement) DeleteAgreement(ctx context.Context, req *model.AgreementIdReq) (res *model.AgreementIdRes, err error) {
	r := g.RequestFromCtx(ctx)
	if _, err = dao.LotAgreement.Ctx(ctx).Where("id", req.Id).Delete(); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	res = &model.AgreementIdRes{
		Id: req.Id,
	}
	return res, err
}
